package com.itheima.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 宋H
 * @Date: 2022/9/3
 * @Description: com.itheima.controller
 * @version: 1.0
 * <p>
 * 通用返回结果，服务端响应的数据最终会封装成此对象
 */
@Data
@Api("返回结果类")
public class R<T> implements Serializable {
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("错误信息")
    private String msg;
    @ApiModelProperty("状态码")
    private Integer code;//状态码
    @ApiModelProperty("动态数据")
    private Map map = new HashMap(); //动态数据

    public static <T> R<T> success(T data) {
        R<T> r = new R<T>();
        r.code = 1;
        r.data = data;
        return r;
    }

    public static <T> R<T> error(String msg) {
        R<T> r = new R<T>();
        r.code = 0;
        r.msg = msg;
        return r;
    }

    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}
